#include "include/i8253.h"
#include "include/i8259.h"
#include "include/irq.h"
#include "include/uart.h"
#include "include/vga.h"
#include "include/mem.h"
#include "include/myPrintk.h"
#include "include/task.h"
#include "../userApp/shell.h"

extern void myMain(void);
extern void ShellMain(void);

void pressAnyKeyToStart(void)
{
	myPrintk(0x5, "Prepare uart device\n");
	myPrintk(0x5, "Then, press any key to start ...\n");

	uart_get_char();
	return;
}

unsigned int sche_flag;
extern int systime;
extern int pre_flag;

void osStart(void)
{
	pressAnyKeyToStart(); // prepare for uart device
	init8259A();
	init8253();
	enable_interrupt();

	clear_screen();

	kMemInit();
	pMemInit(); // after this, we can use kmalloc/kfree and malloc/free

	// finished kernel init
	// NOW, run userApp
	clear_screen();

	char t = 0;
	do
	{
		myPrintk(0x2, "START RUNNING......\n");
		myPrintk(0x2, "Choose Function:\n");
		myPrintk(0x2, "Press [0]: shell\n");
		myPrintk(0x2, "Press [1]: FCFS, disable preemption.\n");
		myPrintk(0x2, "Press [2]: PRIO, disable preemption\n");
		myPrintk(0x2, "Press [3]: SJF,  disable preemption.\n");
		myPrintk(0x2, "Press [4]: PRIO, enable preemption\n");
		t = uart_get_char();
	} while (t < '0' || t > '4');

	clear_screen();

	if (t == '0')
	{
		ShellMain();
	}
	else
	{
		sche_flag=1;
		systime=0;
		switch (t)
		{
		case '1':
			sch_type = FCFS;
			pre_flag=0;
			break;
		case '2':
			sch_type = PRIO;
			pre_flag=0;
			break;
		case '3':
			sch_type = SJF;
			pre_flag=0;
			break;
		case '4':
			sch_type = PRIO;
			pre_flag=1;
			break;
		}
		TaskManagerInit();
	}

	myPrintk(0x2, "STOP RUNNING......ShutDown\n");
	while (1)
		;
}
